package com.example.pclimb.ui.requests;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.TextView;

import com.example.pclimb.R;
import com.example.pclimb.model.ClimbRequest;
import com.example.pclimb.ui.adapters.ClimbRequestAdapter;
import com.example.pclimb.viewmodel.ClimbRequestViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 陪爬人员接取申请页面
 */
public class AvailableRequestsActivity extends AppCompatActivity {

    private static final String TAG = "AvailableRequestsActivity";
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView recyclerRequests;
    private TextView textEmpty;
    private ClimbRequestViewModel viewModel;
    private ClimbRequestAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_available_requests);

        // 设置标题栏
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(v -> onBackPressed());

        // 初始化视图
        swipeRefresh = findViewById(R.id.swipe_refresh);
        recyclerRequests = findViewById(R.id.recycler_requests);
        textEmpty = findViewById(R.id.text_empty);

        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(ClimbRequestViewModel.class);

        // 初始化适配器
        adapter = new ClimbRequestAdapter();
        
        // 设置RecyclerView
        recyclerRequests.setLayoutManager(new LinearLayoutManager(this));
        recyclerRequests.setAdapter(adapter);
        
        // 设置点击事件
        adapter.setOnItemClickListener(request -> {
            try {
                Log.d(TAG, "点击申请，ID: " + request.getId());
                Intent intent = new Intent(this, RequestDetailActivity.class);
                intent.putExtra("request_id", (long)request.getId());
                startActivity(intent);
            } catch (Exception e) {
                Log.e(TAG, "处理申请点击事件出错: ", e);
            }
        });
        
        // 设置下拉刷新
        swipeRefresh.setOnRefreshListener(this::loadData);
        
        // 加载数据
        loadData();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 页面恢复时重新加载数据
        loadData();
    }
    
    /**
     * 加载待匹配申请数据
     */
    private void loadData() {
        // 从ViewModel加载待匹配申请数据
        viewModel.refreshPendingRequests(); // 先刷新数据
        viewModel.getPendingRequests().observe(this, requests -> {
            // 数据加载完成，结束刷新动画
            if (swipeRefresh.isRefreshing()) {
                swipeRefresh.setRefreshing(false);
            }
            
            // 更新UI
            if (requests != null && !requests.isEmpty()) {
                adapter.setRequests(requests);
                textEmpty.setVisibility(View.GONE);
                recyclerRequests.setVisibility(View.VISIBLE);
            } else {
                adapter.setRequests(new ArrayList<>());
                textEmpty.setVisibility(View.VISIBLE);
                recyclerRequests.setVisibility(View.GONE);
            }
        });
    }
} 